package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjNetworkEmpRelation;
import com.zb.entity.ZjNetworkWithdrawWithFeeEmpRoot;
import com.zb.mapper.ZjNetworkWithdrawWithFeeEmpRootMapper;
import com.zb.service.ZjNetworkEmpRelationService;
import com.zb.service.ZjNetworkWithdrawWithFeeEmpRootService;
import com.zb.vo.NetworkWithdrawWithFeeEmpRootUpdateVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 网点提现与费用人员权限表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-06-23
 */
@Service
public class ZjNetworkWithdrawWithFeeEmpRootServiceImpl extends ServiceImpl<ZjNetworkWithdrawWithFeeEmpRootMapper, ZjNetworkWithdrawWithFeeEmpRoot> implements ZjNetworkWithdrawWithFeeEmpRootService {
    @Resource
    private ZjNetworkEmpRelationService networkEmpRelationService;

    // 保存提现与费用人员权限
    @Override
    public void save(Long custAccId, Long networkId, List<Long> withdrawEmpRootVos, List<Long> selectFeeEmpRootVos) {
        deleteByNetworkId(networkId);
        List<ZjNetworkWithdrawWithFeeEmpRoot> withdrawWithFeeEmpRootList = new ArrayList<>();
        ZjNetworkWithdrawWithFeeEmpRoot empRoot = null;
        if (CollectionUtil.isNotEmpty(withdrawEmpRootVos)) {
            for (Long networkEmpId : withdrawEmpRootVos) {
                empRoot = new ZjNetworkWithdrawWithFeeEmpRoot();
                empRoot.setId(IdWorker.getId());
                empRoot.setCustAccId(custAccId);
                empRoot.setNetworkId(networkId);
                empRoot.setType(1);
                empRoot.setNetworkEmpId(networkEmpId);
                withdrawWithFeeEmpRootList.add(empRoot);
            }
        }
        if (CollectionUtil.isNotEmpty(selectFeeEmpRootVos)) {
            for (Long networkEmpId : selectFeeEmpRootVos) {
                empRoot = new ZjNetworkWithdrawWithFeeEmpRoot();
                empRoot.setId(IdWorker.getId());
                empRoot.setCustAccId(custAccId);
                empRoot.setNetworkId(networkId);
                empRoot.setType(2);
                empRoot.setNetworkEmpId(networkEmpId);
                withdrawWithFeeEmpRootList.add(empRoot);
            }
        }
        if (CollectionUtil.isNotEmpty(withdrawWithFeeEmpRootList)) this.saveBatch(withdrawWithFeeEmpRootList);
    }

    // 保存提现与费用人员权限
    @Override
    public void save(Long custAccId, Integer type, NetworkWithdrawWithFeeEmpRootUpdateVo withdrawEmpRootVo) {
        if (CollectionUtil.isEmpty(withdrawEmpRootVo.getNetworkIds())) {
            return;
        }
//        if (!withdrawEmpRootVo.getAllow()) {
//            return;
//        }
        QueryWrapper<ZjNetworkWithdrawWithFeeEmpRoot> wrapper = new QueryWrapper<>();
        wrapper.in("network_id", withdrawEmpRootVo.getNetworkIds())
                .eq("type", type);
        this.remove(wrapper);
        List<ZjNetworkEmpRelation> empRelationList = new ArrayList<>();
        if (withdrawEmpRootVo.getEmpType().stream().anyMatch(f -> f == 2)) {
            QueryWrapper<ZjNetworkEmpRelation> empRelationQueryWrapper = new QueryWrapper<>();
            empRelationQueryWrapper.select("id", "network_id", "network_emp_id")
                    .eq("cust_acc_id", custAccId)
                    .in("network_id", withdrawEmpRootVo.getNetworkIds());
            empRelationList.addAll(networkEmpRelationService.list(empRelationQueryWrapper));
        }
        List<ZjNetworkWithdrawWithFeeEmpRoot> withdrawWithFeeEmpRootList = new ArrayList<>();
        ZjNetworkWithdrawWithFeeEmpRoot empRoot = null;
        for (Long networkId : withdrawEmpRootVo.getNetworkIds()) {
            // 团队负责人
            if (withdrawEmpRootVo.getEmpType().stream().anyMatch(f -> f == 1)) {
                empRoot = new ZjNetworkWithdrawWithFeeEmpRoot();
                empRoot.setId(IdWorker.getId());
                empRoot.setCustAccId(custAccId);
                empRoot.setType(type);
                empRoot.setNetworkId(networkId);
                empRoot.setNetworkEmpId(-1L);
                withdrawWithFeeEmpRootList.add(empRoot);
            }
            // 工程师
            withdrawWithFeeEmpRootList.addAll(empRelationList.stream().filter(f -> Objects.equals(f.getNetworkId(), networkId)).map(emp -> {
                ZjNetworkWithdrawWithFeeEmpRoot empRoot1 = new ZjNetworkWithdrawWithFeeEmpRoot();
                empRoot1.setId(IdWorker.getId());
                empRoot1.setCustAccId(custAccId);
                empRoot1.setType(type);
                empRoot1.setNetworkId(networkId);
                empRoot1.setNetworkEmpId(emp.getNetworkEmpId());
                return empRoot1;
            }).toList());
        }
        if (CollectionUtil.isNotEmpty(withdrawWithFeeEmpRootList)) {
            this.saveBatch(withdrawWithFeeEmpRootList);
        }
    }

    // 根据服务商ID删除
    @Override
    public void deleteByNetworkId(Long networkId) {
        QueryWrapper<ZjNetworkWithdrawWithFeeEmpRoot> wrapper = new QueryWrapper<>();
        wrapper.eq("network_id", networkId);
        this.remove(wrapper);
    }

    // 根据服务商ID获取提现与查询费用人员权限
    @Override
    public List<ZjNetworkWithdrawWithFeeEmpRoot> getByNetworkId(Long networkId) {
        QueryWrapper<ZjNetworkWithdrawWithFeeEmpRoot> wrapper = new QueryWrapper<>();
        wrapper.select("id", "network_id", "type", "network_emp_id")
                .eq("network_id", networkId);
        return this.list(wrapper);
    }

    // 根据服务商ID获取提现与查询费用人员权限
    @Override
    public List<ZjNetworkWithdrawWithFeeEmpRoot> getByNetworkId(Long networkId, Integer type) {
        QueryWrapper<ZjNetworkWithdrawWithFeeEmpRoot> wrapper = new QueryWrapper<>();
        wrapper.select("id", "network_id", "type", "network_emp_id")
                .eq("network_id", networkId).eq("type", type);
        return this.list(wrapper);
    }
}
